package com.nmgsr.ptmsapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.github.abel533.echarts.json.GsonOption;
import com.google.gson.Gson;
import com.nmgsr.ptmsapp.databinding.MymapechartsActivityBinding;
import com.nmgsr.ptmsapp.utils.EchartMapOptionUtil;
import com.nmgsr.ptmsapp.utils.EchartOptionUtil;
import com.nmgsr.ptmsapp.view.EchartView;
import com.nmgsr.ptmsapp.view.EchartViewMap;

import java.util.Random;

public class MyMapEchartsActivity extends AppCompatActivity {
    private EchartViewMap eChartMap;
    private MymapechartsActivityBinding mBinding;
    private EchartView eChart;
    private Handler handler = new Handler();
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = MymapechartsActivityBinding.inflate(getLayoutInflater());
        View view = mBinding.getRoot();
        setContentView(view);

        eChartMap = mBinding.echartMapView;
        mBinding.echartMapView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                refreshEchart();
                // 调用 JavaScript 方法选中呼和浩特市
               // mBinding.echartMapView.evaluateJavascript("selectCity('呼和浩特市');", null);
            }
        });
        // 启用 JavaScript
        WebSettings webSettings = mBinding.echartMapView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        // 初始化 Runnable
        runnable = new Runnable() {
            @Override
            public void run() {
                refreshEchart();
                handler.postDelayed(this, 5000); // 每5秒执行一次
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshEchartMap();
        refreshEchart();
        handler.post(runnable); // 启动定时任务
    }

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacks(runnable); // 停止定时任务
    }

    private void refreshEchartMap() {
        String optionJson = EchartMapOptionUtil.getMapChartOptions("inner_mongolia").toString();
        Log.d("MyMapEchartsActivity", "Option JSON: " + optionJson);
        eChartMap.refreshEchartsWithOption(optionJson);
    }

    private void refreshEchart() {
        Object[] x = new Object[]{
                "1", "2", "3", "4", "5", "6", "7"
        };
        Random random = new Random();
        Object[] y = new Object[7];

        for (int i = 0; i < y.length; i++) {
            y[i] = random.nextInt(100) + 1; // 生成 1 到 100 之间的随机数
        }
        GsonOption option = EchartOptionUtil.getLineChartOptions(x, y);
        Log.d("EchartOption", new Gson().toJson(option));
        mBinding.echartView.refreshEchartsWithOption(new Gson().toJson(option));
    }

    public void onCitySelected(String cityName) {
        // 处理城市选择事件
        //Toast.makeText(this, "Selected City: " + cityName, Toast.LENGTH_SHORT).show();
        // 跳转到对应地点的逻辑
        navigateToLocation(cityName);
    }

    private void navigateToLocation(String cityName) {
        // 实现跳转逻辑，例如启动新的 Activity 或 Fragment
        Log.d("Navigation", "Navigating to: " + cityName);
//        Intent intent = new Intent(MyMapEchartsActivity.this, MyEchatsActivity.class);
//        startActivity(intent);
        runOnUiThread(() -> refreshEchart());
    }
}
